/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package xcore.model.impl;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.InternalEList;

import xcore.constraint.XIntVar;

import xcore.model.ModelPackage;
import xcore.model.XObject;
import xcore.model.XReference;
import xcore.model.XRelation;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>XRelation</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link xcore.model.impl.XRelationImpl#getXObject <em>XObject</em>}</li>
 *   <li>{@link xcore.model.impl.XRelationImpl#getXReference <em>XReference</em>}</li>
 *   <li>{@link xcore.model.impl.XRelationImpl#getObjects <em>Objects</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class XRelationImpl extends EObjectImpl implements XRelation {
	/**
	 * The cached value of the '{@link #getXObject() <em>XObject</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getXObject()
	 * @generated
	 * @ordered
	 */
	protected XObject xObject;

	/**
	 * The cached value of the '{@link #getObjects() <em>Objects</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getObjects()
	 * @generated
	 * @ordered
	 */
	protected EList<XIntVar> objects;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected XRelationImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return ModelPackage.Literals.XRELATION;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public XObject getXObject() {
		if (xObject != null && xObject.eIsProxy()) {
			InternalEObject oldXObject = (InternalEObject)xObject;
			xObject = (XObject)eResolveProxy(oldXObject);
			if (xObject != oldXObject) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, ModelPackage.XRELATION__XOBJECT, oldXObject, xObject));
			}
		}
		return xObject;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public XObject basicGetXObject() {
		return xObject;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setXObject(XObject newXObject) {
		XObject oldXObject = xObject;
		xObject = newXObject;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ModelPackage.XRELATION__XOBJECT, oldXObject, xObject));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public XReference getXReference() {
		if (eContainerFeatureID != ModelPackage.XRELATION__XREFERENCE) return null;
		return (XReference)eContainer();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetXReference(XReference newXReference, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newXReference, ModelPackage.XRELATION__XREFERENCE, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setXReference(XReference newXReference) {
		if (newXReference != eInternalContainer() || (eContainerFeatureID != ModelPackage.XRELATION__XREFERENCE && newXReference != null)) {
			if (EcoreUtil.isAncestor(this, newXReference))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newXReference != null)
				msgs = ((InternalEObject)newXReference).eInverseAdd(this, ModelPackage.XREFERENCE__XRELATIONS, XReference.class, msgs);
			msgs = basicSetXReference(newXReference, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ModelPackage.XRELATION__XREFERENCE, newXReference, newXReference));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<XIntVar> getObjects() {
		if (objects == null) {
			objects = new EObjectContainmentEList<XIntVar>(XIntVar.class, this, ModelPackage.XRELATION__OBJECTS);
		}
		return objects;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case ModelPackage.XRELATION__XREFERENCE:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetXReference((XReference)otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case ModelPackage.XRELATION__XREFERENCE:
				return basicSetXReference(null, msgs);
			case ModelPackage.XRELATION__OBJECTS:
				return ((InternalEList<?>)getObjects()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(NotificationChain msgs) {
		switch (eContainerFeatureID) {
			case ModelPackage.XRELATION__XREFERENCE:
				return eInternalContainer().eInverseRemove(this, ModelPackage.XREFERENCE__XRELATIONS, XReference.class, msgs);
		}
		return super.eBasicRemoveFromContainerFeature(msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case ModelPackage.XRELATION__XOBJECT:
				if (resolve) return getXObject();
				return basicGetXObject();
			case ModelPackage.XRELATION__XREFERENCE:
				return getXReference();
			case ModelPackage.XRELATION__OBJECTS:
				return getObjects();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case ModelPackage.XRELATION__XOBJECT:
				setXObject((XObject)newValue);
				return;
			case ModelPackage.XRELATION__XREFERENCE:
				setXReference((XReference)newValue);
				return;
			case ModelPackage.XRELATION__OBJECTS:
				getObjects().clear();
				getObjects().addAll((Collection<? extends XIntVar>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case ModelPackage.XRELATION__XOBJECT:
				setXObject((XObject)null);
				return;
			case ModelPackage.XRELATION__XREFERENCE:
				setXReference((XReference)null);
				return;
			case ModelPackage.XRELATION__OBJECTS:
				getObjects().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case ModelPackage.XRELATION__XOBJECT:
				return xObject != null;
			case ModelPackage.XRELATION__XREFERENCE:
				return getXReference() != null;
			case ModelPackage.XRELATION__OBJECTS:
				return objects != null && !objects.isEmpty();
		}
		return super.eIsSet(featureID);
	}

} //XRelationImpl
